﻿using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using System;
using System.Drawing;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormium;
using WinFormium.Forms;
using WinFormium.JavaScript;
using WinFormium.WebResource;

namespace MiMiPcNetFrameWork
{
    internal class MainForm : WinFormiumStartup
    {
        protected override MainWindowCreationAction UseMainWindow(MainWindowOptions opts)
        {
            return opts.UseMainFormium<FormConfig>();
        }

        protected override void WinFormiumMain(string[] args)
        {
            global::System.Windows.Forms.Application.EnableVisualStyles();
            global::System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
        }

        protected override void ConfigurationChromiumEmbedded(ChromiumEnvironmentBuiler cef)
        {
        }

        protected override void ConfigureServices(IServiceCollection services)
        {
            // 注册嵌入资源
            services.AddEmbeddedFileResource(new EmbeddedFileResourceOptions
            {
                Scheme = "http",
                DomainName = "embedded.app.local",
                ResourceAssembly = typeof(Program).Assembly,
                EmbeddedResourceDirectoryName = "wwwroot",
            });

            // 注册本地资源
            // Register local resources to specific domain.
            services.AddLocalFileResource(new LocalFileResourceOptions
            {
                Scheme = "http",
                DomainName = "files.app.local",
                PhysicalFilePath = Path.Combine(AppContext.BaseDirectory, "wwwroot"),
            });

            services.AddDataResource("http", "data.app.local", provider =>
            {
                provider.ImportFromCurrentAssembly();
            });
            services.AddWindowBindingObject<JSBing>();
        }
    }
    class FormConfig : Formium
    {
        public FormConfig()
        {
            //本地链接
            Url = "http://files.app.local/index.html";
            // 外部链接  
            //Url = "http://localhost:5173/";
            var form = GetHostWindow();
            form.StartPosition = FormStartPosition.CenterScreen;
            Bitmap iconBm = new Bitmap(Resource._0, new Size(256, 256));
            Icon icon = Icon.FromHandle(iconBm.GetHicon());
            form.Icon = icon;
        }
        protected override void OnPageLoadEnd(PageLoadEndEventArgs args)
        {
            GenMsg();
        }
        private void GenMsg()
        {
            Task.Run(() =>
            {
                while (true)
                {
                    Random random = new Random();
                    // 生成一个0到1之间的随机小数
                    double randomDecimal = random.NextDouble();
                    // 转换为字符串并保留两位小数
                    string randomDecimalString = randomDecimal.ToString("0.00");
                    var Msg = new { Title = "当前值", Data = randomDecimalString, Time = DateTime.Now };
                    EvaluateJavaScriptAsync($"window.$store.dispatch(\"setSever2FrontData\",{JsonConvert.SerializeObject(Msg)});");
                    Thread.Sleep(1000);
                }
            });
        }
        protected override FormStyle ConfigureWindowStyle(WindowStyleBuilder builder)
        {
            var style = builder.UseSystemForm();
            style.WindowState = FormiumWindowState.Normal;
            style.TitleBar = false;
            style.DefaultAppTitle = "甜蜜蜜PC教学";
            style.Sizable = false;
            style.Maximizable = false;
            style.ShowInTaskbar = true;
            style.Size = new Size(1024, 760);
            return style;
        }
        protected override void PaintSplashScreen(PaintEventArgs e)
        {
            Rectangle clipRectangle = e.ClipRectangle;
            Graphics graphics = e.Graphics;
            graphics.Clear(Color.FromArgb(128, 0, 79, 153));
            Bitmap chromium = Resource._0;
            float num = ((clipRectangle.Width / chromium.Width > 3) ? 1f : ((float)clipRectangle.Width / 3f / (float)chromium.Width));
            if (num > 1f)
            {
                num = 1f;
            }
            float num2 = (float)chromium.Width * num;
            float num3 = (float)chromium.Height * num;
            graphics.DrawImage(chromium, new RectangleF(((float)clipRectangle.Width - num2) / 2f, ((float)clipRectangle.Height - num3) / 2f, num2, num3));
            Font font = new Font("华文新魏", 32f, FontStyle.Bold);
            SizeF sizeF = graphics.MeasureString("Powered by 甜蜜蜜", font);
            graphics.DrawString("Powered by 甜蜜蜜", font, Brushes.White, new PointF((float)clipRectangle.Width - sizeF.Width - 20f, (float)clipRectangle.Height - sizeF.Height - 20f));
        }
    }
}
